package com.sinosoft.dsp.platform.components;

import java.util.LinkedList;
import java.util.List;

import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.core.property.BasePropertyHandler;
import com.sinosoft.dsp.system.to.IGenericTO;

/**
 * 这是一个复合的视图类，它能够提供视图类的历史视图
 * 
 * @since 1.0 2010-3-25
 * @author 潘巍（Peter Pan）
 * 
 */
public class ViewHistoryBean extends AbstractViewBean implements
		IViewHistoryBean {

	private static final Log log = Log.getLog(ViewHistoryBean.class.getName());

	private List<IViewBean> vh = new LinkedList<IViewBean>();

	private int currentIndex = -1;

	private int maxLength;

	/**
	 * 是否有下一个视图对象
	 * 
	 * @return
	 */
	@Override
	public boolean hasNext() {
		return getSize() - 1 > currentIndex;
	}

	/**
	 * 是否有上一个视图对象
	 * 
	 * @return
	 */
	@Override
	public boolean hasLast() {
		return currentIndex > 0;
	}

	/**
	 * 移动到下一个视图对象
	 * 
	 * @return
	 */
	@Override
	public IViewBean moveNext() {
		if (hasNext()) {
			currentIndex++;
			return getCurrentView();
		}
		return null;
	}

	/**
	 * 移动到上一个视图对象
	 * 
	 * @return
	 */
	@Override
	public IViewBean moveLast() {
		if (hasLast()) {
			currentIndex--;
			return getCurrentView();
		}
		return null;
	}

	/**
	 * 移动到指定的视图对象
	 * 
	 * @param i
	 * @return
	 */
	@Override
	public IViewBean moveToSpecial(int i) {
		if (i >= 0 && i < getSize()) {
			currentIndex = i;
			return getCurrentView();
		}
		return null;
	}

	/**
	 * 移动到指定的视图对象
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public IViewBean moveToSpecial(String id) {
		return moveToSpecial(indexOf(id));
	}

	/**
	 * 获得当前的视图对象
	 * 
	 * @return
	 */
	@Override
	public IViewBean getCurrentView() {
		try {
			return vh.get(currentIndex);
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * 获得前一个视图对象
	 * 
	 * @return
	 */
	@Override
	public IViewBean getLastView() {
		try {
			return vh.get(--currentIndex);
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * 获得后一个视图对象
	 * 
	 * @return
	 */
	@Override
	public IViewBean getNextView() {
		try {
			return vh.get(++currentIndex);
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * 给定的场景Id是否是当前场景
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public boolean isCurrentView(String id) {
		return id.equals(getCurrentView().getId());
	}

	@Override
	public IViewBean getView(int i) {
		try {
			return this.vh.get(i);
		} catch (Exception e) {
			log.error("通过序号获得子ViewBean出现错误，ID：" + getId() + "，序号：" + i);
		}
		return null;
	}

	@Override
	public List<IViewBean> getAllView() {
		return vh;
	}

	@Override
	public boolean removeView(String id) {
		return removeView(indexOf(id)) == null ? false : true;
	}

	@Override
	public int indexOf(String id) {
		for (int i = 0; i < vh.size(); i++) {
			if (vh.get(i).getId().equals(id)) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public IViewBean removeView(int index) {
		if (index < 0 || index >= getSize()) {
			return null;
		}

		IViewBean rtn = vh.remove(index);

		if (rtn != null) {
			rtn.destroy();
		}

		if (index < currentIndex) {
			currentIndex--;
		} else if (index == currentIndex) {
			if (currentIndex != 0) {
				currentIndex--;
			}
		}
		return rtn;
	}

	@Override
	public boolean addView(IViewBean vb) {
		if (hasAdd()) {
			vh.add(vb);
			currentIndex = (vh.size() - 1);
			return true;
		}
		return false;
	}

	@Override
	public boolean addView(int index, IViewBean vb) {
		if (hasAdd()) {
			vh.add(index, vb);
			currentIndex = index;
			return true;
		}
		return false;
	}

	@Override
	public void init(IGenericTO tos) throws SinoException {
		this.maxLength = Integer.valueOf(BasePropertyHandler
				.getProperty("viewHistoryMaxLength"));
		for (IViewBean v : vh) {
			v.init(tos);
		}
	}

	/**
	 * 是否可以添加元素
	 * 
	 * @return
	 */
	@Override
	public boolean hasAdd() {
		if (getSize() < maxLength) {
			return true;
		}
		return false;
	}

	@Override
	public int getSize() {
		return vh.size();
	}

	@Override
	public void destroy() {
		super.destroy();
		for (IViewBean v : vh) {
			v.destroy();
		}
	}

}
